// pages/order/order.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    topbar1: [{
      id: 0,
      name: '全部'
    }, {
      id: 1,
      name: '待付款'
    }, {
      id: 2,
      name: '待发货'
    }, {
      id: 3,
      name: '待收货'
    }, {
      id: 4,
      name: '待评价'
    }],
    cancel: ['收件人信息有误', '我不想买了', '拍错了', '重复下单', '其他原因'],
    currentTab: 0,
    lists: [],
    show: false,
    uid: '',
    note: '', //取消理由
    piontindex: -1, //理由下标
    orderid: '', //订单id
    page: 1
  },
  // tab切换
  tabswitch: function(e) {
    var that = this;
    let currentTab = e.currentTarget.dataset.idx
    that.setData({
      currentTab: currentTab,
      page: 1
    })
    that.loadorder()
  },
  // 跳快递
  jumpexpress: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/order/express/express?id=' + id,
    })
  },
  // 跳售后
  jumpsales: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/afterSales/applySales/applySales?id=' + id + '&uid=' + this.data.uid,
    })
  },
  // 加载分类订单
  loadorder() {
    let that = this
    if (that.data.currentTab > 0 && that.data.currentTab < 4) {
      wx.request({
        url: app.globalData.api + "getorderlist",
        data: {
          uid: that.data.uid,
          status: that.data.currentTab,
          page: that.data.page
        },
        success: function(res) {
          console.log('订单列表', res)
          if (res.data.code == 1) {
            that.setData({
              lists: []
            })
          } else if (res.data.code == 0 && res.data.list.length == 0) {
            wx.showToast({
              title: '已经到底了',
            })
          } else {
            that.setData({
              lists: that.data.lists.concat(res.data.list),
              page: ++that.data.page
            })
          }
        },
      })
    } else if (that.data.currentTab == 4) {
      wx.request({
        url: app.globalData.api + "getcomment",
        data: {
          uid: that.data.uid,
        },
        success: function(res) {
          console.log('代评价列表', res)
          that.setData({
            lists: res.data.list
          })
        },
      })
    } else {
      wx.request({
        url: app.globalData.api + "getorderlist",
        data: {
          uid: that.data.uid,
          page: that.data.page
        },
        success: function(res) {
          console.log('订单列表', res)
          if (res.data.code == 1) {
            that.setData({
              lists: []
            })
          } else if (res.data.code == 0 && res.data.list.length == 0) {
            wx.showToast({
              title: '已经到底了',
            })
          } else {
            that.setData({
              lists: that.data.lists.concat(res.data.list),
              page: ++that.data.page
            })
          }
        },
      })
    }
  },
  // 提醒发货
  fahuo: function(e) {
    let id = e.currentTarget.dataset.id
    wx.request({
      url: app.globalData.api + "settixingfahuo",
      data: {
        uid: this.data.uid,
        id: id,
      },
      success: function(res) {
        console.log(res)
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      },
    })
  },
  //取消订单
  cancelorder: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    this.setData({
      show: true,
      orderid: id
    })
  },
  //选理由
  choose: function(e) {
    let text = e.currentTarget.dataset.text
    let index = e.currentTarget.dataset.index
    this.setData({
      note: text,
      piontindex: index
    })
  },
  // 确定取消订单
  confirm() {
    var that = this;
    wx.request({
      url: app.globalData.api + "getcancelorder",
      data: {
        uid: that.data.uid,
        id: that.data.orderid,
        note: that.data.note
      },
      success: function(res) {
        console.log('取消订单', res)
        if (res.data.code == 0) {
          that.setData({
            show: false
          })
          wx.showToast({
            title: '已取消',
          })
          that.loadorder()

        } else {
          wx.showToast({
            title: res.data.msg,
          })
        }
      },
    })
  },
  // 删除订单
  delete: function(e) {
    var that = this;
    that.setData({
      show: true
    })
  },
  // 取消
  cancel() {
    var that = this;
    that.setData({
      show: false
    })
  },

  // 删除订单
  deleteorder: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.request({
      url: app.globalData.api + "getdelorder",
      data: {
        uid: that.data.uid,
        id: id
      },
      success: function(res) {
        console.log('删除', res)
        if (res.data.code == 0) {
          wx.showToast({
            title: '删除成功',
          })
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/order/order',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      },
    })
  },
  // 确认收货
  putgood: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    wx.request({
      url: app.globalData.api + "setshouhuo",
      data: {
        id: id,
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 0) {
          wx.showToast({
            title: '收货成功',
          })
          that.loadorder()
        }
      },
    })
  },
  // 跳详情页
  jumpdetail: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/order/orderDetail/orderDetail?id=' + id,
    })
  },
  // 跳评价
  jumpspeak: function(e) {
    let gid = e.currentTarget.dataset.gid
    let oid = e.currentTarget.dataset.oid
    let img = e.currentTarget.dataset.img
    console.log(gid, oid, img)
    wx.navigateTo({
      url: '/pages/speak/dospeak/dospeak?oid=' + oid + '&gid=' + gid + '&img=' + img,
    })
  },
  //支付
  pay: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id
    let price = e.currentTarget.dataset.price
    wx.request({
      url: 'https://bzl.isoft.mobi/index.php/Home/Pay/pay',
      // url: 'http://newbzl.didu86.com/index.php/Home/Pay/pay',
      // url: 'https://bzl.isoft.mobi/index.php/Home/Pay/pay',
      // url: 'https://bzl.isoft.mobi/index.php/Home/PayCeshi/pay',
      data: {
        uid: that.data.uid,
        oid: id,
        price: price
      },
      success: function(res) {
        console.log('支付', res)
        let json = res.data
        console.log(json.appId, json.timeStamp, json.nonceStr, json.package, json.paySign)
        wx.requestPayment({
          appId: json.appId,
          timeStamp: json.timeStamp,
          nonceStr: json.nonceStr,
          package: json.package,
          signType: 'MD5',
          paySign: json.paySign,
          success: function(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/buynow/buysuccess/buysuccess?show=' + 1 + '&price=' + price + '&id=' + id,
            })
          },
          fail: function(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/buynow/buysuccess/buysuccess?show=' + 2 + '&price=' + price + '&id=' + id,
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    that.setData({
      currentTab: options.id
    })
    console.log(options)
    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log(res)
        that.setData({
          uid: res.data.id
        })
        that.loadorder()
      },
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadorder()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})